<?php
/**
 * Created by PhpStorm.
 * User: mr_lee
 * Date: 2017/10/17
 * Time: 上午11:13
 */

namespace app\index\controller;


use app\common\model\Ggk;
use app\common\model\GgkLog;
use app\common\model\Paylog;
use app\common\model\WxUser;
use think\Controller;

class Ggkview extends Controller
{
    public function ggk($orderid="H602-2017101819061187700")
    {
echo $orderid;
        $Paylog=new Paylog();
        $order = $Paylog->where(array('orderid' => $orderid))->find();
        $Ggk=new Ggk();
        $Ggklog=new GgkLog();
        $map['t_mid'] = array('like', "%" . $order['mid'] . "%");
        $map['s_time'] = array('ELT', time());
        $map['e_time'] = array('EGT', time());
        //$map['t_num'] = array('gt', $t_num);
        $id = $Ggk->where($map)->field('id')->select();

        $c = count($id) - 1;
        $gid = rand(0, $c);
        $ggk =$Ggk->where(array('id' => $id[$gid]['id']))->find();
        if ($ggk) {

            $zt = $Ggklog->where(array('orderid' => $orderid))->value('zt');
            if ($zt == '') {
                $data['gid'] = $ggk['id'];
                $data['orderid'] = $orderid;
                $data['openid'] = $order['openid'];
                $data['zt'] = 0;
                $data['addtime'] = time();
                $add = $Ggklog->save($data);
                if ($add) {
                    $Ggk->where(array('id' => $ggk['id']))->setInc('f_num');
                    return $ggk;

                } else {

                    return false;
                }
            } elseif ($zt == 0) {
                return $ggk;

            } else {

                return false;
            }
        } else {

            return false;
        }


    }


    public function ggklq()
    {
        $requset=request();
        if ($requset->isAjax()) {
            $Ggk=new Ggk();
            $Ggklog=new GgkLog();
            $WxUser=new WxUser();
            $map['orderid'] = I('post.orderid');
            $data['zt'] = 1;
            $up = $Ggklog->where($map)->save($data);

            if ($up) {
                $openid = $Ggklog->where($map)->field('openid,gid')->find();

                $jifen =$Ggk->where(array('id' => $openid['gid']))->value('j_num');
                $WxUser->where(array('openid' => $openid['openid']))->setInc('jifen', $jifen);
                echo 1;
            } else {
                echo 2;
            }

        }

    }

}